﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BusinessLogic;
using System.Web.Security;
using System.Data.SqlClient;
using System.Data;

namespace DataAccessLogic
{
    public static class NoteDataAccess
    {
        private static Note BindRowToNote(DataRow row)
        {
            Note n = new Note();
            n.ID = DataAccess.CheckForGuidColumn(row, "ID");
            n.Text = DataAccess.CheckForStringColumn(row, "TEXT");
            n.ConstituentID = DataAccess.CheckForGuidColumn(row, "CONSTITUENTID");
            n.DateAdded = DataAccess.CheckForDateTimeColumn(row, "DATEADDED");
            n.DateChanged = DataAccess.CheckForDateTimeColumn(row, "DATECHANGED");
            n.AddedByID = DataAccess.CheckForGuidColumn(row, "ADDEDBYID");
            n.ChangedByID = DataAccess.CheckForGuidColumn(row, "CHANGEDBYID");
            n.AddedBy = DataAccess.CheckForStringColumn(row, "ADDEDBY");
            n.ChangedBy = DataAccess.CheckForStringColumn(row, "CHANGEDBY");

            return n;
        }

        public static Guid Create(Note note)
        {
            Guid id = Guid.Empty;
            Guid providerUserKey = new Guid(Membership.GetUser().ProviderUserKey.ToString());
            string outputParameterName = "@ID";

            if (note != null)
            {
                List<SqlParameter> parms = new List<SqlParameter>();
                SqlParameter p = new SqlParameter();
                p.ParameterName = outputParameterName;
                p.DbType = System.Data.DbType.Guid;
                p.Direction = System.Data.ParameterDirection.Output;

                parms.Add(p);
                parms.Add(new SqlParameter("@CONSTITUENTID", note.ConstituentID));
                parms.Add(new SqlParameter("@TEXT", note.Text));
                parms.Add(new SqlParameter("@ASPNETDBUSERID", providerUserKey));

                object obj = DataAccess.Create(StoredProcedures.USP_NOTE_CREATE, outputParameterName, parms);

                if (obj != null)
                {
                    id = new Guid(obj.ToString());
                }
            }

            return id;
        }

        public static void Delete(Guid id)
        {
            if (id != Guid.Empty)
            {
                DataAccess.Delete(StoredProcedures.USP_NOTE_DELETE, id);
            }
        }

        public static List<Note> GetAllByConstituentID(Guid constituentID)
        {
            List<Note> notes = null;

            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@CONSTITUENTID", constituentID));

            DataTable dt = DataAccess.Get(StoredProcedures.USP_NOTE_GETBYCONSTITUENTID, parms);

            if (null != dt)
            {
                notes = new List<Note>();

                foreach (DataRow row in dt.Rows)
                {
                    notes.Add(BindRowToNote(row));
                }
            }

            return notes;
        }

        public static void Update(Note note)
        {
            Guid providerUserKey = new Guid(Membership.GetUser().ProviderUserKey.ToString());

            if (note != null)
            {
                List<SqlParameter> parms = new List<SqlParameter>();
                parms.Add(new SqlParameter("@ID", note.ID));
                parms.Add(new SqlParameter("@TEXT", note.Text));
                parms.Add(new SqlParameter("@CONSTITUENTID", note.ConstituentID));
                parms.Add(new SqlParameter("@ASPNETDBUSERID", providerUserKey));

                DataAccess.Update(StoredProcedures.USP_NOTE_UPDATE, parms);
            }
        }
    }
}